package NewTmas.Listeners;

import NewTmas.FramesandPanels.OpsModeMainFrame;
import taskmaster.job.Task;
import taskmaster.job.TaskCollection;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: jhauser
 * Date: Jun 17, 2011
 * Time: 1:15:24 PM
 * To change this template use File | Settings | File Templates.
 */
public class OpsPropsActionListener implements ActionListener
{

    private  TaskCollection taskCollection;
    Task task;
    String taskTag;
    String jobTag;
    JDesktopPane dtp;


    public OpsPropsActionListener(String jobName, String task, TaskCollection taskCollection,JDesktopPane dtp) {
        this.taskCollection=taskCollection;
        this.taskTag=task;
        this.jobTag=jobName;
        this.dtp=dtp;
    }


    public void actionPerformed(ActionEvent e1)
    {
        JInternalFrame frame=new JInternalFrame( "PROPERTIES :"+taskTag+":"+jobTag) ;
        frame.setSize(500,400);
        JTextArea  textArea= new JTextArea();
        textArea.setEditable(false);

        task=taskCollection.getTask(jobTag,taskTag);


        textArea.append("Description:   " + task.getDescription() + "\n\n")  ;
        textArea.append("Command:    " + task.getCommand() + "\n\n");
        textArea.append("Iteration:         " + task.getIteration() + "\n\n");
        textArea.append("Type:               "+task.getType()+"\n\n");
        textArea.append("Auto Retries:   " + task.getRetries() + "\n\n");
        textArea.append("Timeout:           "+task.getTimeout()+"\n\n");

        textArea.append("Role:           "+ OpsModeMainFrame.roleHash.get(String.valueOf(task.getRoleId())));
        textArea.setEditable(false);
        frame.add(new JScrollPane(textArea));
        frame.setVisible(true);
        frame.setClosable(true);
        frame.setIconifiable(true);
        frame.setResizable(true);
        frame.setMaximizable(true);
        frame.getContentPane().setBackground(Color.WHITE);


        JButton PredViewButton = new JButton("View Predecessors");
//        PredViewButton.setContentAreaFilled(false);
//        PredViewButton.setBorderPainted(false);

        JButton SuccViewButton = new JButton("View Successors");
//        SuccViewButton.setContentAreaFilled(false);
//        SuccViewButton.setBorderPainted(false);

        GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                        .addComponent(PredViewButton)
                                        .addComponent(SuccViewButton))
                                .addContainerGap(456, Short.MAX_VALUE))
                        .addComponent(textArea, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE)
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addComponent(textArea, GroupLayout.PREFERRED_SIZE, 250, GroupLayout.PREFERRED_SIZE)
                                .addGap(18)
                                .addComponent(PredViewButton)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(SuccViewButton)
                                .addContainerGap(58, Short.MAX_VALUE))
        );
        frame.getContentPane().setLayout(groupLayout);


        SuccViewButton.addActionListener(new ViewSuccPredTaskListener(task,taskCollection,jobTag,dtp));
        PredViewButton.addActionListener(new ViewSuccPredTaskListener(task,taskCollection,jobTag,dtp));



        dtp.add(frame) ;
        frame.toFront();

    }

}
